
#include "PreHeader.h"
#include "cEntity.h"

cEntity::cEntity()
{
	m_ID = INVALID_ID;
	m_body = NULL;
	m_sprite = NULL;
	m_parent = NULL;

	m_deleted = false;
}

cEntity::~cEntity()
{
	m_parent->removeChild(m_sprite, true);
}

void cEntity::Init(const sEntityDesc& desc)
{
	m_ID = desc.ID;
	m_body = desc.body;
	m_parent = desc.parent;

	// load Sprite
	m_sprite = CCSprite::spriteWithFile(desc.spriteFilename.c_str());
	
	// to do
	// null check

	m_parent->addChild(m_sprite);
}

void cEntity::Update(float dt)
{
	const b2Vec2& pos = m_body->GetPosition();
	m_sprite->setPosition(ccp(pos.x*PTM_RATIO, pos.y*PTM_RATIO));
}

void cEntity::SetPosv(const CCPoint& posv)
{
	m_body->SetLinearVelocity(b2Vec2(posv.x/PTM_RATIO, posv.y/PTM_RATIO));
}

void cEntity::SetPos(const CCPoint& pos)
{
	m_body->SetTransform(b2Vec2(pos.x/PTM_RATIO, pos.y/PTM_RATIO), 0.0f);
}